package com.bjsxt.shoppingOnLine.shopping_service_impl.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shoppingOnLine.shopping_service_api.pojo.AAdmins;
import com.bjsxt.shoppingOnLine.shopping_service_api.pojo.ARole;
import com.bjsxt.shoppingOnLine.shopping_service_api.service.IUsersService;
import com.bjsxt.shoppingOnLine.shopping_service_impl.dao.IAAdminsDao;
import com.bjsxt.shoppingOnLine.shopping_service_impl.dao.IARoleDao;
import com.bjsxt.shoppingOnline.shopping_common.util.ConstatFinalUtil;
import com.bjsxt.shoppingOnline.shopping_common.util.PageInfoUtil;

@Service("usersService")
public class UsersServiceImpl implements IUsersService
{
	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		
		int i=1;
		return this.adminsDao.findOne(condMap);
	}
	@Override
	public JSONObject updateOneAdminsService(AAdmins admins)
	{
		int res=this.adminsDao.update(admins);
		JSONObject resultJSON=new JSONObject();
		if(res>0)
		{
			resultJSON.put("code","0");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("0"));
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("id", admins.getId());
		dataJSON.put("effect", res);
		resultJSON.put("data", dataJSON);
		}else{
			
			resultJSON.put("code","2");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins)
	{
		int res=this.adminsDao.delete(admins);
		JSONObject resultJSON=new JSONObject();
		if(res>0)
		{
			resultJSON.put("code","0");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("0"));
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("id", admins.getId());
		dataJSON.put("effect", res);
		resultJSON.put("data", dataJSON);
		}else{
			
			resultJSON.put("code","3");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins)
	{
		int res=this.adminsDao.save(admins);
		JSONObject resultJSON=new JSONObject();
		
		if(res>0)
		{
			resultJSON.put("code","0");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("0"));
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("id", admins.getId());
		dataJSON.put("effect", res);
		resultJSON.put("data", dataJSON);
		}else{
			
			resultJSON.put("code","1");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		if(condMap.get("keyword")!=null&&!"".equals(condMap.get("keyword"))){
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil!=null){
			Map<String, Object> countMap=new HashMap<String,Object>();
			countMap.put("page", "true");
			
			List<AAdmins> list=this.adminsDao.findCondList(condMap);
			int totalRecord=0;
			if(list.size()>0)
			{
				totalRecord=list.get(0).getId();
			}
			
		
			pageInfoUtil.setTotalRecord(totalRecord);
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageCount", "false");
			condMap.put("pageSize", pageInfoUtil.getPageSize());
		}
		return this.adminsDao.findCondList(condMap);
	}

	
	
	/**
	 * 
	 * 
	 * 角色操作开始==========================
	 * 
	 * 
	 */
	
	@Override
	public ARole findOneRoleService(Map<String, Object> condMap)
	{
		
		
		return this.roleDao.findOne(condMap);
	}
	@Override
	public JSONObject updateOneRoleService(ARole Role)
	{
		int res=this.roleDao.update(Role);
		JSONObject resultJSON=new JSONObject();
		if(res>0)
		{
			resultJSON.put("code","0");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("0"));
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("id", Role.getId());
		dataJSON.put("effect", res);
		resultJSON.put("data", dataJSON);
		}else{
			
			resultJSON.put("code","2");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	@Override
	public JSONObject deleteOneRoleService(ARole Role)
	{
		int res=this.roleDao.delete(Role);
		JSONObject resultJSON=new JSONObject();
		if(res>0)
		{
			resultJSON.put("code","0");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("0"));
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("id", Role.getId());
		dataJSON.put("effect", res);
		resultJSON.put("data", dataJSON);
		}else{
			
			resultJSON.put("code","3");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	@Override
	public JSONObject saveOneRoleService(ARole Role)
	{
		int res=this.roleDao.save(Role);
		JSONObject resultJSON=new JSONObject();
		
		if(res>0)
		{
			resultJSON.put("code","0");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("0"));
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("id", Role.getId());
		dataJSON.put("effect", res);
		resultJSON.put("data", dataJSON);
		}else{
			
			resultJSON.put("code","1");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		if(condMap.get("keyword")!=null&&!"".equals(condMap.get("keyword"))){
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil!=null){
			Map<String, Object> countMap=new HashMap<String,Object>();
			countMap.put("page", "true");
			
			List<ARole> list=this.roleDao.findCondList(condMap);
			int totalRecord=0;
			if(list.size()>0)
			{
				totalRecord=list.get(0).getId();
			}
			
		
			pageInfoUtil.setTotalRecord(totalRecord);
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageCount", "false");
			condMap.put("pageSize", pageInfoUtil.getPageSize());
		}
		return this.roleDao.findCondList(condMap);
	}
	
	
}
